// 04myCenter/04wasteOrder/02wasteOrderDetail/wasteOrderDetail.js
var co = require('../../../resource/js/common.js')
var ws = require('../../../resource/js/websocket.js')
var app = getApp()
var _this, _data
Page({

   /**
    * 页面的初始数据
    */
   data: {
      orderID: '', //订单ID
      orderDetail: '', //订单详情
      schedule: '' //进度
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      _this = this
      _data = this.data
      this.setData({
         orderID: options.orderID
      })
      
   },
   //获取订单详情
   getOrderDetail: function() {
      var url = 'order/Order/getOrderDetails'
      var param = {
         Order_ID: _data.orderID
      }
      co.getJyhsApi(url, param, function(res) {
         if (res.code == 0) {
            _this.setData({
               orderDetail: res.data
            })
            //进度条设置
            if (res.data.Order_Status == 0) { //未接单
               _this.setData({
                  schedule: 0
               })
            } else if (res.data.Order_Status == 1) { //已接单
               _this.setData({
                  schedule: 1
               })
            } else if (res.data.Order_Status == 2) { //已完成
               _this.setData({
                  schedule: 4
               })

            } else if (res.data.Order_Status == 3) { //已评价
               _this.setData({
                  schedule: 5
               })

            } else if (res.data.Order_Status == 4) { //已支付
               _this.setData({
                  schedule: 3
               })

            } else if (res.data.Order_Status == 5) { //待上门
               _this.setData({
                  schedule: 2
               })
            }
         }
      })
   },
   //联系回收人
   callPhone: function() {
      var phone = _data.orderDetail.Reclaim_User_Info.User_Mobile
      wx.makePhoneCall({
         phoneNumber: phone,
      })
   },
   //订单操作
   operateOrder: function(e) {
      //console.log('eeee',e)
      var url = 'order/Order/updateOrderStatue'
      var status = e.currentTarget.dataset.status
      var orderID = e.currentTarget.dataset.order
      if (status == 1 || status == 0) {
         var doType = 'cancelOrder'//取消订单
         var afterStatus = -1
      } else if (status == 4) {
         var doType = 'finishOrder'//完成订单
         var afterStatus = 2
      }
      var param = {
         Order_ID: orderID,
         Order_Status: afterStatus
      }
      co.getJyhsApi(url, param, function (res) {
         if (res.code == 0) {
            wx.showToast({
               title: '订单操作成功',
               icon: 'success',
               duration: 1000
            })
                    
            _this.getOrderDetail()//操作成功重新获取订单详情
            _this.ws_send(doType, res.data.Reclaim_User_ID)//取消成功发送websocke
            if (afterStatus == -1) {
               //console.log(-1)
               wx.navigateBack({
                  delta: 1
               })
               // return
            }
         }
      })

   },
   //用户发送web
   ws_send: function (doType, Reclaim_User_ID) {
      var msg = {
         type: doType,//'cancelOrder',
         reclaim_id: Reclaim_User_ID,
      }
      console.log('ws_send_order msg=====', msg)
      ws.sendSocketMsg(JSON.stringify(msg), function (res) {
         console.log('【sendSocketMsg:发送成功回调（111）】')
      })
   },
   //跳到评价页
   toEvaluate: function(e) {
      var orderID = e.currentTarget.dataset.order.Order_ID
      var recID = e.currentTarget.dataset.order.Reclaim_User_ID
      wx.navigateTo({
         url: '/04myCenter/04wasteOrder/01wasteOrderEvaluate/wasteOrderEvaluate?orderID=' + orderID + '&recID=' + recID,
      })
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
      this.getOrderDetail()
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },
})